# -*- coding: utf-8 -*-

from gamestate import *
import unittest
from game_state_statisics import GameStateStatistics

EPS = 1e-3


class GameStateStatisticsTest(unittest.TestCase):

    def setUp(self):
        self.game_state = None
        self.rounds = []
        first, second = Player('Petya', 'Petya'), Player('Vasya', 'Vasya')
        self.rounds.\
            append(GameRound(None,
                             [WordGuessResult(None,
                                              WordGuessResult.
                                              GUESS, i)
                              for i in (1, 2, 4, 5, 8)],
                             None, (first, second)))
        first, second = Player('Kirill', 'Pdetya'), Player('Dima', 'Vdasya')
        self.rounds.\
            append(GameRound(None,
                             [WordGuessResult(None,
                                              WordGuessResult.
                                              GUESS, i)
                              for i in (5, 9, 11, 17, 25)],
                             None, (first, second)))

    def test_get_average_time_per_round(self):
        self.assertEqual(GameStateStatistics.
                         get_average_guessing_time_per_round(self.rounds[0]),
                         1.6)
        self.rounds[0].words = []
        self.assertEqual(GameStateStatistics.
                         get_average_guessing_time_per_round(self.rounds[0]),
                         0)

    def test_get_average_time(self):
        self.assertEqual(GameStateStatistics.
                         get_average_guessing_time(self.rounds), 3.3)

    def test_average_time_for_pair(self):
        self.assertLess(abs(
            GameStateStatistics.get_average_guessing_time(
                self.rounds, ('Vasya', 'Petya')) - 1.6), EPS)

    def test_dispersion(self):
        self.assertLess(abs(
            GameStateStatistics.get_dispersion_per_round(
                self.rounds[0]) - 1.066), EPS)

    def test_min(self):
        self.assertEqual(GameStateStatistics.get_min_guessing_time(self.rounds), 1)

    def test_max(self):
        self.assertEqual(GameStateStatistics.get_max_guessing_time(self.rounds), 8)


if __name__ == '__main__':
    unittest.main()
